package com;

import java.io.File;

import serverFileManager.FileManager;

public class SDFSProtocol {		

	public static final String FILEDIR = "./ServerFiles/";
	//public static final String GETFILEDIR = "/home/moaad/Desktop/GetFile/";
	//public static final String PUTFILEDIR = "/home/moaad/Desktop/PutFile/";

	public String state = "DEFAULT";

	String fileToPut = null;

	public byte[] processRequest(byte[] inBytes, String clientID) {

		if (state.equals("DEFAULT")) {
			String inString = new String(inBytes);

			if (inString.substring(0, 4).equals("GET ")) {

				String fileName = inString.substring(4);	
				//System.out.println("*****LOG PROTOCOL : GETTING *" + fileName + "*");
				return processGetFile(fileName, clientID);

			} else if (inString.substring(0, 4).equals("PUT ")) {

				fileToPut = inString.substring(4);	
				//System.out.println("*****LOG PROTOCOL : PUTTING *" + fileToPut + "*");
				state = "WAITING_FOR_FILE";
				return "!WAITING PUT!".getBytes();

			} else if (inString.substring(0, 4).equals("DELE")) {
				String[] delegationCmd = inString.split(" ");

				int duration = new Integer(delegationCmd[3]);

				return processDelegation(clientID, delegationCmd[1], delegationCmd[2], duration);

			} else if (inString.substring(0, 4).equals("EXIT")) {

				return null;

			} else {
				System.out.println("*****COMMAND UNKNOWN");
				return new byte[0];
			}

		} else if (state.equals("WAITING_FOR_FILE")) {

			//System.out.println("LOG PROTOCOL :  WAITING_FOR_FILE");
			System.out.println("*****Waiting for file...");
			state="DEFAULT";			 

			return processPutFile(clientID, inBytes, fileToPut);

		} else {

			System.out.println("*****ne devrait pas passer par la !!!");
			return "CONNECTED".getBytes();
		}
	}


	public byte[] processGetFile(String fileName, String clientID) {	
		File file = new File(FILEDIR + fileName);

		if ( file.exists() ) {

			System.out.println("*****Sending: " + FILEDIR + fileName);

			byte[] fileBytes = "!FILE NOT FOUND!".getBytes();

			try {			
				fileBytes = FileManager.getFile(fileName, clientID);

				if (fileBytes != null) {
					return fileBytes;
				} else {
					return "!NO ACCESS TO FILE!".getBytes();
				}

			} catch (Exception e) {
				System.out.println("*****CANNOT ACCESS TO FILE");
				return "!NO ACCESS TO FILE!".getBytes();
			}

		} else {
			System.out.println("*****File not found on the server : " + FILEDIR + fileName);
			return "!FILE NOT FOUND!".getBytes();
		}
	}


	public byte[] processPutFile(String clientID, byte[] inputBytes, String fileName) {
		long startTime = System.currentTimeMillis();

		try {
			FileManager.storeFile(clientID, fileName, inputBytes);

			System.out.println("*****File saved at : " + FILEDIR + fileName);

		} catch (Exception e) {
			return "!PUT FILE FAILED!".getBytes();
		}

		long finishTime = System.currentTimeMillis();

		System.out.println("*****Stroring the file took: "+ (finishTime-startTime) + " ms");


		
		return "!PUT FILE SUCCEDED!".getBytes();
	}


	public byte[] processDelegation(String clientID, String fileName, String receiver, int duration){
		System.out.println("*****Processing delegation for : " + fileName + " " + receiver + " " + duration);

		if ( FileManager.delegateRights(clientID, fileName, receiver, duration)) {
			return "!DELEGATION SUCCEEDED!".getBytes();
		} else {
			return "!DELEGATION FAILED!".getBytes();
		}
	}

}